//
//  AIManager.m
//  AI
//
//  Created by 甘世清 on 2023/7/9.
//

#import "Manager.h"
#import "ViewController.h"
#import "LoginViewController.h"
#import "FristLBViewController.h"
@implementation Manager
+(UIViewController*)SQ_getViewController{
    UIViewController * vc ;
        if ([[UserModel share].token isNotEmpty]) {
           NSString * lb = [Manager SQ_stringForKey:@"FristLB"];
            if ([lb isNotEmpty]) {
                vc =  [[ViewController  alloc]init];
            } else {
                vc =  [[FristLBViewController  alloc]init];
            }
        } else {
             //未登录
            vc = [[LoginViewController alloc]init];
        }
    return vc;
}

+ (UIViewController*)SQ_getCurrentViewController{

  UIViewController* vc = [UIApplication sharedApplication].keyWindow.rootViewController;
  while (1) {
    if ([vc isKindOfClass:[UITabBarController class]]) {
        vc = ((UITabBarController*)vc).selectedViewController;
    }
    if ([vc isKindOfClass:[UINavigationController class]]) {
        vc = ((UINavigationController*)vc).visibleViewController;
    }
    if (vc.presentedViewController) {
        vc = vc.presentedViewController;
    }else{
        break;
    }
   }
   return vc;
}

/// 根据key存储值
/// @param value 值
/// @param key key
+(void)SQ_SetObject:(NSString*)value FormKey:(NSString*)key
{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    [user setObject:value forKey:key];
    [user synchronize];
}
/// 根据key删除值
/// @param key key
+(void)SQ_removeObjectForKey:(NSString*)key
{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    [user removeObjectForKey:key];
    [user synchronize];
}
/// 根据key取值
/// @param key KEY
+(NSString*)SQ_stringForKey:(NSString*)key
{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    return [user stringForKey:key];
}
/// 根据数据判断去对应的根视图
+(void)SQ_goToVC{

        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc]init]];
        [Manager SQ_getCurrentViewController].view.window.rootViewController = nav;
}

/**
 *  获取上一个月的时间
 */
+ (NSString *)getLastMonth:(int)year
{
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *lastMonthComps = [[NSDateComponents alloc] init];
        [lastMonthComps setYear:year]; // year = 1表示1年后的时间 year = -1为1年前的日期，month day 类推
//    [lastMonthComps setMonth:-1];
    NSDate *newdate = [calendar dateByAddingComponents:lastMonthComps toDate:currentDate options:0];
    NSString *dateStr = [formatter stringFromDate:newdate];
//    NSLog(@"date str = %@", dateStr);
    return dateStr;
}

/// 时间
/// @param time 时间戳
+(NSString * )SQ_getTimeString:(NSInteger)time
{
    int new = [[NSString SQ_currentTime] intValue];
    int index = new - (int)time;;
    if (index < 60) {
        return @"Just";
    }
    int mins = index/60;
     if(mins < 60){
        return [NSString stringWithFormat:@"%d mins ago",mins];
    }
    int hous =  index/ 3600;
    if(hous < 24){
       return [NSString stringWithFormat:@"%d hours ago",hous];
   }
    int day = index/(24*3600);
    if (day <30) {
        return [NSString stringWithFormat:@"%d day ago",day];
    }
    return  [NSString SQ_time:[NSString stringWithFormat:@"%ld",(long)time] Formatter:@"yyyy-MM-dd"];
}


+(NSString*)SQ_getStringID
{
    NSString * idstring = [Manager SQ_stringForKey:@"account"];
    if ([idstring isNotEmpty]) {
        return  idstring;
    }
//    return  @"1708617579_3703900F-6F24-490E-9CFE-EE82908E5EA5";
    NSString * ID = [NSString stringWithFormat:@"%@_%@",[NSString SQ_currentTime],[NSString aliyun_generateUUID]];
    return ID;
}
 

/**
 对数组里存放model的某个属性对数组进行重新排序

 @param array 待排序数组
 @param increase 是否增序排列
 @param key 属性
 @return <#return value description#>
 */
+(NSArray *)sortArray:(NSArray *)array increase:(BOOL)increase byKey:(NSString *)key{
    
    array = [array sortedArrayUsingComparator:
             ^NSComparisonResult(MessageModel *obj1, MessageModel *obj2) {
                 NSComparisonResult result;
                 if ([key isEqualToString:@"userid"]) {
                     CGFloat res1 = [[obj1 valueForKey:key] floatValue];
                     CGFloat res2 = [[obj2 valueForKey:key] floatValue];
                     if (increase) {
                         if (res1 < res2){
                             result = NSOrderedAscending;
                         }else {
                             result = NSOrderedDescending;
                         }
                     } else {
                         if (res1 < res2){
                             result = NSOrderedDescending;
                         }else {
                             result = NSOrderedAscending;
                         }
                     }
                 } else {
                     result = [[obj1 valueForKey:key] compare:[obj2 valueForKey:key]];
                     if (!increase) {
                         if (result == NSOrderedDescending) {
                             result = NSOrderedAscending;
                         } else if (result == NSOrderedAscending) {
                             result = NSOrderedDescending;
                         }
                     }
                 }
                 
                 return result;
             }].mutableCopy;
    return array;
}

+ (CGSize)SQ_boundingAttributedStringRectWithSize:(CGSize)size
                                     lineSpace:(CGFloat)lineSpace
                                      textFont:(UIFont *)textFont
                                        string:(NSString *)string
{
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.lineSpacing = lineSpace;
    NSDictionary *attribute = @{NSFontAttributeName:textFont,
                                NSParagraphStyleAttributeName:paraStyle};
    return [string boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
}

@end
